package io.fft.flight.modules.flight.controller;

import io.fft.flight.common.annotation.LogOperation;
import io.fft.flight.common.constant.Constant;
import io.fft.flight.common.page.PageData;
import io.fft.flight.common.utils.Result;
import io.fft.flight.common.validator.AssertUtils;
import io.fft.flight.common.validator.ValidatorUtils;
import io.fft.flight.common.validator.group.AddGroup;
import io.fft.flight.common.validator.group.DefaultGroup;
import io.fft.flight.common.validator.group.UpdateGroup;
import io.fft.flight.modules.flight.dto.OrderDTO;
import io.fft.flight.modules.flight.dto.order.OrderNotifyBaseDTO;
import io.fft.flight.modules.flight.dto.order.OrderNotifyDTO;
import io.fft.flight.modules.flight.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.Map;


/**
 * 用户订单
 *
 * @author likailun kellenli30541@gmail.com
 * @since 1.0.0 2023-06-20
 */
@RestController
@RequestMapping("flight/order")
@Api(tags = "用户订单")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({@ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"), @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"), @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"), @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String")})
    @RequiresPermissions("flight:order:page")
    public Result<PageData<OrderDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<OrderDTO> page = orderService.page(params);

        return new Result<PageData<OrderDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("flight:order:info")
    public Result<OrderDTO> get(@PathVariable("id") Long id) {
        OrderDTO data = orderService.get(id);

        return new Result<OrderDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("flight:order:save")
    public Result save(@RequestBody OrderDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        orderService.saveDTO(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("flight:order:update")
    public Result update(@RequestBody OrderDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        orderService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("flight:order:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        orderService.delete(ids);

        return new Result();
    }

    @PostMapping("/notify")
    @ResponseBody
    @ApiOperation("就旅行订单通知")
    @LogOperation("订单通知")
    public Map<String, String> notify(@RequestBody OrderNotifyDTO dto) {
        //效验数据
        boolean b = orderService.orderNotify(dto);
        final Map<String, String> result = new HashMap<>();
        result.put("rsCode", b ? "000000" : "001001");//请求返回代码，成功000000,失败001001
        result.put("rsMessage", "");//返回已知的错误信息
        return result;
    }

    @PostMapping("/queryDetail/{orderNo}")
    @ResponseBody
    @ApiOperation("订单明细")
    public Result<OrderNotifyBaseDTO> queryDetail(@PathVariable("orderNo")String orderNo) {
        //效验数据
        OrderNotifyBaseDTO result = orderService.queryDetail(orderNo);
        return new Result<OrderNotifyBaseDTO>().ok(result);
    }


}